<!-- Banner管理 -->
<template>
  <div style="padding-left:20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-cascader :options="typeList" v-model="data.type" size="medium" ref="update" @change="changeCascader" clearable :props="childrenList"></el-cascader>
        <!-- <el-select v-model="data.type" size="medium" clearable class="iup_width200" placeholder="请选择类别">
          <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="search">{{$t('lan.query')}}</el-button>
        <el-button type="info" size="medium" @click="reset">{{$t('lan.reset')}}</el-button>
        <el-button type="primary" size="medium" @click="add('create','')">{{$t('lan.add')}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="getList" v-loading="loading" :element-loading-text="$t('lan.dataLoading')" empty-text border stripe>
      <div slot="empty">
        <div v-if="loading"></div>
        <div v-else>{{$t('lan.available')}}</div>
      </div>
      <el-table-column type="index" :label="$t('lan.num')" align="center" width="140"></el-table-column>
      <el-table-column prop="code" :label="$t('lan.mainTitle')" align="center"></el-table-column>
      <el-table-column prop="name" :label="$t('lan.subTitle')" align="center"></el-table-column>
      <el-table-column prop="typeName" :label="$t('lan.column')" align="center"></el-table-column>
      <el-table-column prop="targetUrl" :label="$t('lan.pictureAddress')" align="center"></el-table-column>
      <el-table-column prop="url" :label="$t('lan.imgAddress')" align="center">
        <template slot-scope="scope">
          <el-image class="viewer-img" :src="scope.row.url" :preview-src-list="[scope.row.url]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column :label="$t('lan.operation')" width="180" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="add('edit',scope.row)">{{$t('lan.modify')}}</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)">{{$t('lan.del')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <pagination :total="total" :pageData="data" @hanleSize="changePage" @hanlePage="handleSizeChange"></pagination>
    <!-- 添加 /修改管理参数 -->
    <BannerListModal v-if="BannerData.showModal" :BannerData="BannerData" @closeModal="getRoleList" />
  </div>
</template>
<script>
import BannerListModal from 'component/permissions/BannerListModal'
export default {
  components: {
    BannerListModal
  },
  data () {
    return {
      typeList: [],
      getList: [],
      data: {
        pageSize: 10,
        currentPage: 1,
        type: '',
      },
      total: 0,
      loading: false,
      getTime: '',
      BannerData: {},
      childrenList: {
        expandTrigger: 'click',
        children: 'subCategoryBOList',
        value: 'id',
        label: 'name'
      },
    }
  },
  methods: {
    changeCascader () {
      let data = this.$refs.update.getCheckedNodes(true)
      if (data != '') {
        this.data.type = data[0].data.id
      }
    },

    // 获取字典表
    async getterm () {
      const url = '/category/queryList'
      const result = await this.request.httpRequest('POST', url, { pid: 0 });
      if (result.code === 200) {
        this.typeList = result.data
      }
    },
    //点击页数触发
    changePage (page) {
      this.data.currentPage = page
      this.getRoleList();
    },
    //选择页数最多显示几条数据
    handleSizeChange (val) {
      this.data.pageSize = val
      this.getRoleList();
    },
    //查询角色
    changeInput () {
      this.data.currentPage = 1
      this.getTime();
    },
    //查询关键字
    search () {
      this.data.currentPage = 1
      this.getRoleList();
    },
    //重置列表
    reset () {
      Object.assign(this.$data.data, this.$options.data().data)//重置列表
      this.getRoleList();
    },
    // 获取banner列表
    async getRoleList () {
      this.loading = true
      let url = '/banner/queryPage'
      let result = await this.request.httpRequest('POST', url, this.data)
      if (result.code == 200) {
        this.total = result.data.totalRows
        this.data.pageSize = result.data.pageSize
        this.data.currentPage = result.data.currentPage
        this.getList = result.data.datas
      } else {
        this.$message({ type: 'error', message: result.msg })
      }
      this.loading = false;
    },
    //添加/修改 banner
    add (type, row) {
      this.BannerData = {
        title: type == 'create' ? this.$t('lan.newBanner') : this.$t('lan.modifyBanner'),
        showModal: true,
        userId: row.id || '',
        type: type
      }
    },
    //删除用户列表
    del (row) {
      this.$confirm(this.$t('lan.delBanner'), this.$t('lan.prompt'), {
        confirmButtonText: this.$t('lan.confirm'),
        cancelButtonText: this.$t('lan.cancel'),
        type: 'warning'
      }).then(async () => {
        this.loading = true;
        let url = `/banner/delete/${row.id}`
        let result = await this.request.httpRequest('DELETE', url)
        if (result.code == 200) {
          this.$message({ type: 'success', message: result.msg });
          this.getRoleList();
        } else {
          this.loading = false
          this.$message({ type: 'error', message: result.msg })
        }
      })
    },
  },
  mounted () {
    this.getterm();
    this.getRoleList();
    this.getTime = this.helper.debounce(this.getRoleList)//防抖
  },
  created () {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        this.search();
      }
    };
  }
}
</script>
<style scoped>
.el-form {
  padding: 15px 0px !important;
}
.el-form-item {
  margin-bottom: 0px !important;
}
.logoimg {
  width: 50px;
  height: 50px;
}
</style>